// pages/user/user/changeUserInfo/changeUserInfo.js
var base = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      changeusername:"",
      username:"",
      changephone:"",
      phone:"",
      changeaddress:"",
      address:"",
      openid:"",
      avatarUrl:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAll();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

 changeInfo:function(e){
    this.setData({
      changeusername: e.detail.value.changeusername,
      changephone:e.detail.value.changephone,
      changeaddress:e.detail.value.changeaddress,
    })
    wx.request({
      url: 'http://localhost:8080/wx/user/getInfo1/'+base.globalData.openid,
      success:(res) => {
      //  console.log(res)
        if(res.data.data) {
          wx.request({
            url: 'http://localhost:8080/wx/user',
            data: {
              username:this.data.changeusername,
              phone:this.data.changephone,
              address:this.data.changeaddress,
              userid:res.data.data.userid,
            },
            method:"PUT",
          })
          base.globalData.username=this.data.changeusername
          base.globalData.address=this.data.changeaddress
          base.globalData.phone=this.data.changephone        
        }
          else {
            wx.request({
              url: 'http://localhost:8080/wx/user',
              data: {
                username:this.data.changeusername,
                phone:this.data.changephone,
                address:this.data.changeaddress,
                weixinOpenid:base.globalData.openid
              },
              method:"POST",
            })
            base.globalData.username=this.data.changeusername
            base.globalData.address=this.data.changeaddress
            base.globalData.phone=this.data.changephone
            const userInfo = wx.getStorageSync("userInfo")
          //  console.log(userInfo)
            base.globalData.avatarUrl=userInfo.avatarUrl
          }
        }
      })  
      this.getAll();
      wx.showToast({
        title: '修改成功',
        con:"success"
      })
      setTimeout(function () {
        // login
        wx.switchTab({
          url: '../../../user/user',
        })
      }, 1000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAll();
  },

  getAll:function(){
  //  console.log("修改信息中的getAll()")
  const userInfo = wx.getStorageSync("userInfo");
    this.setData({
      phone:base.globalData.phone,
      username:base.globalData.username,
      address:base.globalData.address,
      avatarUrl:base.globalData.avatarUrl,
      avatarUrl:userInfo.avatarUrl,
    })
   // console.log(base.globalData.username)
   /* const openid=base.globalData.openid;   
    console.log(openid)
    wx.request({
      url: 'http://localhost:8080/findById',
      data: {openid:base.globalData.openId},
      success:(result)=>{
        console.log(result)
        wx.setStorageSync('username', result.data.username)
        wx.setStorageSync('phone', result.data.phone)
        wx.setStorageSync('address', result.data.address)

        this.setData({
          username:wx.getStorageSync('username'),
          gender:wx.getStorageSync('phone'),
          birthday:wx.getStorageSync('address'),
        })
      }
    })*/
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})